package com.cms.gdm.service.impl;

import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cms.common.utils.R;
import com.cms.gdm.dao.ProcessDao;
import com.cms.gdm.entity.ProcessEntity;
import com.cms.gdm.entity.vo.StudentProcessListVo;
import com.cms.gdm.feign.StuFeign;
import com.cms.gdm.to.StudentTo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cms.common.utils.PageUtils;
import com.cms.common.utils.Query;

import com.cms.gdm.dao.StudentProcessDao;
import com.cms.gdm.entity.StudentProcessEntity;
import com.cms.gdm.service.StudentProcessService;

import javax.annotation.Resource;


@Service("studentProcessService")
public class StudentProcessServiceImpl extends ServiceImpl<StudentProcessDao, StudentProcessEntity> implements StudentProcessService {

    @Resource
    private ProcessDao processDao;

    @Resource
    private StuFeign stuFeign;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<StudentProcessEntity> page = this.page(
                new Query<StudentProcessEntity>().getPage(params),
                new QueryWrapper<StudentProcessEntity>()
        );

        List<StudentProcessEntity> records = page.getRecords();
        List<StudentProcessListVo> result = new ArrayList<>();

        for (StudentProcessEntity record : records) {
            StudentProcessListVo studentProcessListVo = new StudentProcessListVo();
            BeanUtils.copyProperties(record, studentProcessListVo);
            ProcessEntity processEntity = processDao.selectById(record.getProcessId());
            studentProcessListVo.setProcessTitle(processEntity.getTitle());
            R r = stuFeign.selectInfoById(record.getStudentId());
            StudentTo student = r.getData("student", new TypeReference<StudentTo>() {
            });
            studentProcessListVo.setStudentName(student.getName());
            result.add(studentProcessListVo);
        }

        IPage<StudentProcessListVo> resultPage =  new Page<>();
        resultPage.setRecords(result);
        resultPage.setCurrent(page.getCurrent());
        resultPage.setPages(page.getPages());
        resultPage.setSize(page.getSize());
        resultPage.setTotal(page.getTotal());

        return new PageUtils(resultPage);
    }

    @Override
    public StudentProcessEntity infoByStudentId(Long id) {
        StudentProcessEntity studentProcessEntity = baseMapper.selectOne(new QueryWrapper<StudentProcessEntity>().eq("student_id", id));
        return studentProcessEntity;
    }

}